package com.xiangyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyang.annotations.OperationLog;
import com.xiangyang.entity.Role;
import com.xiangyang.entity.dto.Result;
import com.xiangyang.mapper.IRoleMapper;
import com.xiangyang.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends ServiceImpl<IRoleMapper, Role> implements IRoleService {

    @Autowired
    private IRoleMapper irp;

    @Override
    @OperationLog(title = "角色管理", businessType = "添加角色")
    public Result addRole(Role role) {
        boolean inserted = irp.insert(role) > 0;
        if (!inserted) {
            return Result.error("角色添加失败");
        }
        return Result.ok();
    }

    @Override
    @OperationLog(title = "角色管理", businessType = "删除角色" )
    public Result deleteRole(Long roleId) {
        // 非空判断
        if (roleId == null) {
            return Result.error("角色ID不能为空");
        }
        // 删除角色信息
        int deleted = irp.deleteById(roleId);
        if (deleted == 0) {
            return Result.error("角色删除失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateRole(Role role) {
        // 非空判断
        if (role == null || role.getRoleId() == null || role.getRoleName() == null || role.getRoleChar() == null) {
            return Result.error("角色信息不完整");
        }
        // 更新角色信息
        int updated = irp.updateById(role);
        if (updated == 0) {
            return Result.error("角色更新失败");
        }
        return Result.ok();
    }

    @Override
    public Result getRoleList() {
        // 实现查询列表逻辑
        return Result.ok(irp.selectList(null));
    }

    @Override
    public Result<Object> getRoleList(int pageNum, int pageSize) {
        // 分页查询
        if (pageNum <= 0 || pageSize <= 0) {
            return Result.error("参数不合法");
        }
        Page<Role> page = new Page<>(pageNum, pageSize);
        Page<Role> rolePage = irp.selectPage(page, new QueryWrapper<>());
        if (rolePage == null || rolePage.getRecords().isEmpty()) {
            return Result.error("没有查询到角色信息");
        }
        return Result.ok(rolePage);
    }
}